package domotica.server;

import java.util.List;
import domotica.house.automation.ActuatorSensorSimulator;
import domotica.house.automation.Parameter;
import domotica.house.simulation.utils.PhysicVariableSimulator;
import domotica.house.simulation.utils.TaskSimulator;

/**
 * Questa classe simula un irroratore per il giardino, che misurata l'umidita' del terreno puo' alzarne il valore fino ad una certa soglia.
 */
public class GardenWatererSimulator extends ActuatorSensorSimulator {
	
	private TaskSimulator taskSimulator;
	
	public GardenWatererSimulator(String name, String measureName,
			PhysicVariableSimulator physicSimulator) {
		super(name, measureName, physicSimulator);
	}
	
	/**
	 * Metodo per la definizione di un task per il componente.
	 */
	public synchronized void addTask(List<String> parametersList) {
		
		taskSimulator = new TaskSimulator(this, parametersList, this.physicSimulator){

			private boolean interrupt=false;
			
			public void run() {
				
				double desiredHumidity = Double.parseDouble(this.getParametersList().get(0));
				double variationForSecond = Double.parseDouble(this.getParametersList().get(1))/51;
			
				while (!interrupt){
					if (this.getPhysicSimulator().getPhysicVariable()<desiredHumidity){
						this.getPhysicSimulator().changePhysicVariable(variationForSecond);
						System.out.println(this.getActuatorSensor().getName()+" sta lavorando per voi");
						System.out.println(this.getActuatorSensor().getMeasureName()+" aumentata di " + variationForSecond);
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				
				this.getActuatorSensor().setOn(false);
				
			}

			@Override
			public void interrupt() {
				this.interrupt=true;				
			}
			
		};
		
		Thread taskThread  = new Thread(taskSimulator);
		taskThread.start();
		
		this.setOn(true);
	}

	public void setParametersList() {
		this.parametersNames.add(new Parameter("Umidita' desiderata", 0, 100));
		this.parametersNames.add(new Parameter("Velocita' irroramento desiderata",1,51));
	}
	
	/**
	 * Metodo che permette di interrompere il task del componente
	 */
	public synchronized void stopTask() {
		taskSimulator.interrupt();
		this.setOn(false);		
	}

}
